package com.fengye.aop.demo.config;

import com.fengye.aop.demo.base.IBaseWorkflowProcessor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fengyexjtu@126.com
 * @since 2022-10-01
 */
@Service
public class WorkflowStrategyProcessorRegistry implements ApplicationContextAware {
    
    Map<Integer, IBaseWorkflowProcessor> processorMap = new HashMap<>();
    
    @Value("${workflow_engine:1}")
    public Integer code;
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, IBaseWorkflowProcessor> beans = applicationContext.getBeansOfType(IBaseWorkflowProcessor.class);
        if (!beans.isEmpty()){
            for (String key : beans.keySet()) {
                IBaseWorkflowProcessor bean = beans.get(key);
                processorMap.put(bean.getType(), bean);
            }
        }
    }
    
    public IBaseWorkflowProcessor getType() {
        return processorMap.get(code);
    }
}
